package com.android.djlmapp.activity.kaika;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.djlmapp.R;
import com.android.djlmapp.adapter.kaika.JiTuanBianMaAdapter;
import com.android.djlmapp.application.MyApplication;
import com.android.djlmapp.http.GlobalUrls;
import com.android.djlmapp.model.kaika.JiTuanOrGuHuaInfo;
import com.android.djlmapp.tools.global.ToastUtils;
import com.android.djlmapp.tools.weight.MyCityItemDecoration;
import com.google.gson.Gson;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class KaiKaPopupWindowActivity extends Activity {

    @BindView(R.id.kaika_iv_close)
    ImageView kaikaIvClose;
    @BindView(R.id.ed_jtbm)
    EditText edJtbm;
    @BindView(R.id.recycle_jtbm)
    RecyclerView recyclerView;
    @BindView(R.id.tv_jt_title)
    TextView tvJtTitle;
    @BindView(R.id.tv_jt_name)
    TextView tvJtName;
    @BindView(R.id.rl_progress)
    RelativeLayout rlProgress;

    private int isJiTuan = 0; // 是集团编码还是固话号码
    private boolean isFlag = false; // 是集团编码还是固话号码
    private String url = "";
    private JiTuanBianMaAdapter jiTuanBianMaAdapter;
    private List<JiTuanOrGuHuaInfo.ListBean> mList = new ArrayList<>();
    private String keyvalue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_kai_ka_popup_window);
        ButterKnife.bind(this);
        initView();
        initData();
    }

    protected void initView() {
        isJiTuan = getIntent().getIntExtra("isJiTuan", 0);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);

        if (isJiTuan == 1) {
            isFlag = true;
            tvJtTitle.setText("集团编码");
            tvJtName.setText("集团编码");
            edJtbm.setHint("请输入集团编码");
            url = GlobalUrls.GET_KAIKA_JITUAN_DATA;
        } else if (isJiTuan == 2) {
            isFlag = false;
            tvJtTitle.setText("固话号码");
            tvJtName.setText("固话号码");
            edJtbm.setHint("请输入固话号码");
            url = GlobalUrls.GET_KAIKA_GUHUA_DATA;
        }
    }

    protected void initData() {
        jiTuanBianMaAdapter = new JiTuanBianMaAdapter(KaiKaPopupWindowActivity.this, isFlag);
        //设置item间距，15dp
        recyclerView.addItemDecoration(new MyCityItemDecoration(KaiKaPopupWindowActivity.this,
                LinearLayoutManager.HORIZONTAL, 2, ContextCompat.getColor(KaiKaPopupWindowActivity.this,
                R.color.homeTitleBottom)));

        jiTuanBianMaAdapter.setList(mList);
        // 设置Adapter
        recyclerView.setAdapter(jiTuanBianMaAdapter);

        jiTuanBianMaAdapter.setBackClickValueListener(new JiTuanBianMaAdapter.BackClickValueListener() {
            @Override
            public void backClickValue(String value, String addPara) {
                edJtbm.setText(value);

                Intent intent = new Intent();
                intent.putExtra("JTBM_RESULT", value);
                intent.putExtra("ADDPARA_RESULT", addPara);
                setResult(RESULT_OK, intent);
                finish();
                overridePendingTransition(R.anim.push_left_in, R.anim.push_right_out);
            }
        });

        // 监听软键盘回车事件
        edJtbm.setOnEditorActionListener(new TextView.OnEditorActionListener() {

            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {

                if (actionId == EditorInfo.IME_ACTION_SEND
                        || actionId == EditorInfo.IME_ACTION_DONE
                        || actionId == EditorInfo.IME_ACTION_NONE
                        || actionId == EditorInfo.IME_ACTION_GO
                        || actionId == EditorInfo.IME_ACTION_SEARCH
                        || actionId == EditorInfo.IME_ACTION_NEXT
                        || (event != null && KeyEvent.KEYCODE_ENTER
                        == event.getKeyCode() && KeyEvent.ACTION_DOWN
                        == event.getAction())) {

                    //处理事件
                    keyvalue = edJtbm.getText().toString().trim();
                    if (keyvalue != null && !TextUtils.isEmpty(keyvalue)) {
                        getData(); // 获取数据
                    }
                }
                return true;
            }
        });
        getData(); // 获取数据
    }

    private void getData() {
        keyvalue = edJtbm.getText().toString().trim();
        rlProgress.setVisibility(View.VISIBLE);

        MyApplication.getInstance().getHttpTools().getJiTuanNumOrGuHuaNum(url, keyvalue, new StringCallback() {

            @Override
            public void onSuccess(Response<String> response) {
                try {
                    parseJson(response.body());
                } catch (Exception e) {
                    e.printStackTrace();
                    ToastUtils.showShortToast(KaiKaPopupWindowActivity.this, "数据解析异常");
                }
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                String body = response.body();
                Log.e("KaiKaPopup数据", "-----onError------" + body);
                ToastUtils.showShortToast(KaiKaPopupWindowActivity.this, "网络或服务器错误");
            }

            @Override
            public void onFinish() {
                super.onFinish();
                rlProgress.setVisibility(View.GONE);
            }
        });
    }

    private void parseJson(String json) {
        Log.e("KaiKaPopup数据", keyvalue + "-----parseJson------" + json);
        Gson gson = new Gson();
        JiTuanOrGuHuaInfo jiTuanOrGuHuaInfo = gson.fromJson(json, JiTuanOrGuHuaInfo.class);
        if (jiTuanOrGuHuaInfo != null) {
            if (jiTuanOrGuHuaInfo.getCode() < 1000) {
                mList.clear();
                List<JiTuanOrGuHuaInfo.ListBean> list = jiTuanOrGuHuaInfo.getList();
                if (list != null && list.size() > 0) {
                    mList.addAll(list);
                    jiTuanBianMaAdapter.setList(mList);
                } else {
                    ToastUtils.showShortToast(KaiKaPopupWindowActivity.this, "未获取到数据!");
                }
            } else {
                ToastUtils.showShortToast(KaiKaPopupWindowActivity.this, jiTuanOrGuHuaInfo.getMsg());
            }
        } else {
            ToastUtils.showShortToast(KaiKaPopupWindowActivity.this, "未获取到数据");
        }
    }

    private boolean isSure = false;

    @OnClick(R.id.kaika_iv_close)
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.kaika_iv_close:
                finish();
                break;
        }
    }
}
